package com.tools.commons.enums;

import org.springframework.util.Assert;

import java.util.stream.Stream;


public interface TypeEnum<T> {

    static <V, E extends TypeEnum<V>> E typeToEnum(Class<E> enumType, V type) {
        Assert.notNull(enumType, "enum type must not be null");
        Assert.notNull(type, "type must not be null");
        Assert.isTrue(enumType.isEnum(), "type must be an enum type");

        return Stream.of(enumType.getEnumConstants())
                .filter(item -> item.getType().equals(type))
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException("unknown database type: " + type));
    }

    T getType();
}
